package com.nanhang.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;

import com.nanhang.common.util.R;
import com.nanhang.entity.Classes;
import com.nanhang.entity.Student;
import com.nanhang.service.ClassesService;
import com.nanhang.service.StudentService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;
    @Autowired
    private ClassesService classesService;
    @GetMapping("/list")
    public R findAll(){
        List<Student> list = studentService.list();
        return R.ok().data("students",list);
    }
    @GetMapping("/list1")
    public R findAll1(){
        List<Student> students = studentService.list();

        students.forEach(st -> {
            QueryWrapper<Classes> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("cid", st.getCid());
            Classes classes = classesService.getOne(queryWrapper);
            st.setCname(classes.getCname());
        });
        return R.ok().data("students",students);
    }
    @GetMapping("/findByPage")
    public IPage<Student> findByPage(@RequestParam("pagenum") Integer pagenum, @RequestParam("pageSize") Integer pageSize){
        IPage<Student> page1 = new Page<>(pagenum, pageSize);
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("sex", "男");

        return studentService.page(page1,queryWrapper);
    }
}
